package web

import (
	"net/http"
	"strings"
)

func GetParams(r *http.Request) map[string]string {
	resultMap := make(map[string]string)
	if err := r.ParseForm(); err != nil {
		return map[string]string{}
	} else {
		for key, value := range r.Form {
			switch len(value) {
			case 1:
				resultMap[key] = value[0]
			default:
				resultMap[key] = strings.Join(value, "|")
			}
		}
		return resultMap
	}
}

func GetRemoteAddress(r *http.Request) string {
	ip := r.Header.Get("remote_addr")
	if ip == "" {
		ip = strings.Split(r.RemoteAddr, ":")[0]
	}
	//处理本地监听0.0.0.0的请求
	if ip == "[" {
		ip = "127.0.0.1"
	}
	return ip
}
